
#[cfg(test)]
mod test {

    // 更新字符串。尝试用索引来操作字符串
    #[test]
    fn test_string_update() {
        let s = String::from("foobar");
        let mut result = s.into_bytes();
        (0..result.len()).for_each(|i|
            if i % 2 == 0 {
                result[i] = result[i].to_ascii_lowercase();
            }else {
                result[i] = result[i].to_ascii_uppercase();
            }
        );

        assert_eq!("fOoBaR", String::from_utf8(result).unwrap());
    }

    // 更新字符串。按字符迭代来处理字符串
    #[test]
    fn test_string_update1() {
        let s = String::from("foobar");

        // 该段代码有问题，编译失败
        /*let s: String = s.chars().enumerate().map(|(i, c)| {
                if i % 2 == 0 {
                    c.to_lowercase().to_string();
                }else {
                    c.to_uppercase().to_string();
                }
            }).collect();

        assert_eq!("fOoBaR", s);*/
    }

    #[test]
    fn test_r_string() {
        let s = r"1234
                        5678
                        9876
                        4321";
        let (mut x, mut y) = (0, 0);
        for(idx, val) in s.lines().enumerate() {
            let val = val.trim();
            let left = val.get(idx..idx + 1)
                .unwrap().parse::<u32>().unwrap();
            let right = val.get((3 - idx)..(3 - idx + 1))
                .unwrap().parse::<u32>().unwrap();

            x += left;
            y += right;
        }
        assert_eq!(38, x + y);
    }
}